<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">
                    <i class="layui-icon layui-icon-add-circle"></i>新增
                </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">
                    <i class="layui-icon layui-icon-delete"></i>删除
                </button>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>


<script>
    layui.use(['form', 'table','miniPage','miniAdmin','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniAdmin = layui.miniAdmin,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            method: 'post',
            url: 'repair/query_my_room',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 120, title: 'ID', sort: true},
                {field: 'repItem', title: '维修物件'},
                {field: 'description', minWidth: 300, title: '描述'},
                {field: 'repDate', title: '申请日期'},
                {field: 'stuId', title:'申请人', templet:'<div>{{d.user.userName}}</div>'},         // 根据repair表中的学生ID查user表中的学生姓名
                {field: 'buildingId', title:'楼栋号', templet: function (row) {
                        return row.buildingId + '栋';
                    }},
                {field: 'buildingName', title:'楼栋名', templet: '<div>{{d.building.buildingName}}</div>'},
                {field: 'roomId', title: '房间ID', templet:'<div>{{d.room.brand}}</div>'},
                {field: 'repStatus', title: '维修状态', sort: true, templet: function (row) {
                        if (row.repStatus === 0){
                            return '<span class="layui-badge layui-bg-silk-silver">等待维修⏳</span>';
                        } else if (row.repStatus === 1){
                            return '<span class="layui-badge layui-bg-silk-yellowgreen">完成维修✨</span>';
                        } else if (row.repStatus === 2){
                            return '<span class="layui-badge layui-bg-orange">这娃儿骗人😤</span>';
                        }
                    }}
            ]],
            limits: [10, 15, 20, 30, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });


        window.reload = function () {
            table.reload('currentTableId');     // 刷新表格
        }


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);

            //----------------
            // 弹出（去掉）
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            //执行搜索重载
            table.reload('currentTableId', {
                where: data.field
            }, 'data');

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {                                  // 🌸监听新增操作
                let content = miniPage.getHrefContent('repairstu/add.html');
                let index = layer.open({
                    title: '添加申请',
                    type: 1,
                    shade: 0.3,
                    maxmin:true,                    // 显示最大最小化的按钮
                    shadeClose: true,
                    area: ['600px', '600px'],       // 原始值 area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: 'auto',                 // 原始值 offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {                            // 🌸监听修改操作
                let data = table.checkStatus('currentTableId').data;
                if (data.length === 0){
                    layer.msg("还没有选中数据");
                } else{
                    layer.confirm("真的要删除吗", function (index) {
                        let arr = data.map(item=>item.id);                   // 异步删除(包括map, filter, some, every)

                        axios.get('repair/delete?ids=' + arr.join(",")).then(function (response) {
                            table.reload('currentTableId');
                            layer.msg(response.msg);
                        }).catch(function (error) {
                            layer.msg(error);
                        });
                        layer.close(index);
                    })
                }
            }
        });


        function setFormValue(data) {               // 给弹出的修改表单赋值
            form.val("updateForm", {
                id: data.id,
                userName: data.userName,
                gender: data.gender,
                email: data.email,
                roomId: data.roomId,
                userType: data.userType,
            })
        }


    });
</script>